package com.test.qpbgboot.base.service.impl;

import com.test.qpbgboot.base.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean checkValue(String key, String value) {
        if (value == null) {
            return redisTemplate.opsForValue().get(key) == null;
        }
        return value.equals(redisTemplate.opsForValue().get(key));
    }

    @Override
    public void setValue(String key, String value) {
        log.debug("redis储存数据业务:{},{}", key, value);
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setValue(String key, String value, long timeout, TimeUnit timeUnit) {
        log.debug("redis储存定时过期数据业务:{},{},过期时间:{},{}", key, value, timeout, timeUnit);
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }
}
